package com.wo.cloud.code.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wo.cloud.code.dto.SysDataSourceDto;
import com.wo.cloud.code.entity.SysDataSource;
import com.wo.cloud.code.service.ISysDataSourceService;
import com.wo.cloud.core.constant.SysConstant;
import com.wo.cloud.core.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/6/17 15:42
 * Description: 数据库 库数据信息操作
 */

@RestController
@RequestMapping(SysConstant.SYS_DATA_SOURCE_BASE_URL)
@Api(tags = "数据库库数据")
public class SysDataSourceController {

    @Autowired
    private ISysDataSourceService sysDataSourceService;

    // 新增数据库信息
    @PostMapping
    @ApiOperation(value = "新增数据库库信息")
//    @Log(value = "新增|更新数据库库信息")
    public Response<SysDataSource> create(@RequestBody SysDataSource sysDataSource) {
        sysDataSourceService.saveOrUpdate(sysDataSource);
        return Response.data(sysDataSource);
    }

    // 数据库 库数据分页展示
    @PutMapping
    @ApiOperation(value = "分页展示库信息")
//    @Log(value = "分页展示库信息")
    public Response<IPage<SysDataSource>> page(Page page, SysDataSourceDto sysDataSourceDto) {
        IPage<SysDataSource> sourcePage = sysDataSourceService.getSourcePage(page, sysDataSourceDto);
        return Response.data(sourcePage);
    }

    // 数据库 库信息 列表展示
    @GetMapping
    @ApiOperation(value = "列表展示库信息")
//    @Log(value = "列表展示库信息")
    public Response<List<SysDataSource>> list() {
        List<SysDataSource> sysDataSources = sysDataSourceService.list();
        return Response.data(sysDataSources);
    }

    // 数据库 删除操作
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据库库信息")
//    @Log(value = "删除数据库库信息")
    public Response delete(@PathVariable("id") String dataSourceId) {
        boolean removeById = sysDataSourceService.removeById(dataSourceId);
        if (removeById) {
            return Response.success();
        }
        return Response.fail();
    }

    // 获取库数据详情
    @GetMapping("/{id}")
    @ApiOperation(value = "获取数据库详情")
//    @Log(value = "获取数据库详情")
    public Response getById(@PathVariable("id") String dataSourceId) {
        SysDataSource sysDataSource = sysDataSourceService.getById(dataSourceId);
        return Response.data(sysDataSource);
    }


}
